// component/moneySearch/moneySearch.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    calendarShow: false,
    minDate: new Date(2020, 1, 1).getTime(),
    maxDate: new Date().getTime() + 1000 * 60 * 60 * 24,
    form: {
      isAsc: 'asc',
      name: '',
      tel: '',
      isDisable: 1,
      nickName: '',
      depeId: '',
      createTime: '',
      updateTime: '',
      affiliated: '',
    },
    disabledIndex: 1,
    depeIdIndex: '',
    date: '',
    searchIndex: '',
    disabledOption: [{value: 0, name: '禁用'},{value: 1,name: '启用'}],
    selectData: [
      {
        'id': 1,
        'statusName': '框架部'
      },
      {
        'id': 2,
        'statusName': '玻璃部'
      },
      {
        'id': 3,
        'statusName': '纱窗部'
      }, {
        'id': 4,
        'statusName': '工程部'
      },
      {
        'id': 5,
        'statusName': '服务部'
      },
      {
        'id': 6,
        'statusName': '后勤部'
      },
      {
        'id': 7,
        'statusName': '已完成'
      }
    ],
    deptOption: [],
  },

  created() {
    this.getDepartment();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getDepartment() {
      wx.request({
        url: `${app.globalData.baseUrl}/api/finance/getDictDatas`,
        method: "GET",
        header: {
          'token': wx.getStorageSync('token'),
          "Content-Type": "application/json"
        },
        data: {
          // dictType: 'sys_affiliated_unit',
        },
        success: (res) => {
          if (res.data.code == 0) {
            this.setData({
              deptOption: res.data.data,
            });
          }
        }
      })
    },
    nameInput(e) {
      this.setData({ 
        form: {
          ...this.data.form,
          name: e.detail
        } 
      });
    },
    telInput(e) {
        this.setData({ 
            form: {
              ...this.data.form,
              tel: e.detail
            } 
        });
    },
    disabledChange(e) {
      var mchObj = this.data.disabledOption[e.detail.value];
      this.setData({
        disabledIndex: e.detail.value,
        form: {
          ...this.data.form,
          isDisable: mchObj.value
        } 
      })
    },
    userNameInput(e) {
      this.setData({ 
        form: {
          ...this.data.form,
          nickName: e.detail
        } 
      });
    },
    statusChange(e) {
      var mchObj = this.data.selectData[e.detail.value];
      this.setData({
        depeIdIndex: e.detail.value,
        form: {
          ...this.data.form,
          depeId: mchObj.statusName
        } 
      })
    },
    deptChange(e) {
      var mchObj = this.data.deptOption[e.detail.value];
      this.setData({
        searchIndex: e.detail.value,
        form: {
          ...this.data.form,
          affiliated: mchObj.dictValue
        } 
      })
    },
    onDisplay() {
      this.setData({ calendarShow: true });
    },
    onRangeClose() {
      this.setData({ calendarShow: false });
    },
    formatDate(date) {
      date = new Date(date);
      let month = date.getMonth() + 1;
      let day = date.getDate();
      if(month < 10) {
        month = '0' + month;
      }
      if(day < 10) {
        day = '0' + day;
      }
      return `${date.getFullYear()}/${month}/${day}`;
    },
    onConfirm(event) {
      const [start, end] = event.detail;
      const endTime = new Date(end).getTime();
      this.setData({
        calendarShow: false,
        form: {
          ...this.data.form,
          createTime: this.formatDate(start),
          updateTime: this.formatDate(endTime),
        },
        date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
      });
    },
    reset() {
      this.setData({
        form: {
          isAsc: 'asc',
          name: '',
          tel: '',
          isDisable: 1,
          nickName: '',
          depeId: '',
          createTime: '',
          updateTime: '',
          affiliated: '',
        },
        date: '',
        depeIdIndex: '',
        disabledIndex: 1,
        searchIndex: ''
      });
      this.triggerEvent('resetIsPage');
      this.triggerEvent('search', {...this.data.form});
    },
    search() {
      var data = {...this.data.form};
      this.triggerEvent('search', data);
    },
    onClose() {
      this.triggerEvent('onClose');
    }
  }
})
